#include <iostream>
using namespace std;
//对于这个两层结构其实就是使用另一个类对destory进行了封装
class Singleton{

public:
    static Singleton * getInstance(){
        if(_ps==nullptr){
            _ps=new  Singleton ();
        }
        return _ps;
    }
    void print(){
    cout<<i<<endl;
    }
    friend class Autorelease_updata;
    friend class Autorelease;
private:
    int i=10;
    static    Singleton * _ps;
    Singleton(){
        cout<<"singleton"<<endl;
    }
    ~Singleton(){
        cout<<"~Singleton"<<endl;
    }
};
Singleton * Singleton::_ps=nullptr;

class Autorelease{
public:
    Autorelease (Singleton *ps)
        :_ps(ps){
        }
    ~Autorelease (){
        if(_ps){
            delete [] _ps;
            _ps=nullptr;
        }
    }
private:
    Singleton* _ps;
};

class Autorelease_updata{
public:
   Autorelease_updata(Singleton* p)
        {
        cout<<"Autorelease_updata"<<endl;
        }

    ~Autorelease_updata(){
        cout<<"~Autorelease_updata"<<endl;
        if(Singleton::_ps){
        delete Singleton::_ps;
        Singleton::_ps=nullptr;
        }
    }

};
void test(){
    //Autorelease ar(Singleton::getInstance());
   // Autorelease ar2(Singleton::getInstance());
    

}
void test1(){
    Autorelease_updata ar(Singleton::getInstance());
    Autorelease_updata ar2(Singleton::getInstance());
    Singleton::getInstance()->print();
}
int main(int argc, char * argv[]){
    test();
    test1();
    return 0;
}

